﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Mechanical;
using YArchitech.Controls;
using YArchitech.LIB;
using YArchitech.Revit;

namespace YArchitech.HYSpace
{
	public class ZoneInfoManager
	{
		public ZoneInfoManager(Document document)
		{
			this.doc = document;
		}

		private List<Space> GetAllSpaces()
		{
			List<Space> result;
			try
			{
				List<Space> list = new List<Space>();
				foreach (Element element in ((IEnumerable<Element>)YJKElementFilter.GetElementByCategory(this.doc, BuiltInCategory.OST_MEPSpaces)))
				{
					Space space = element as Space;
					if (space != null)
					{
						list.Add(space);
					}
				}
				result = list;
			}
			catch (Exception)
			{
				result = null;
			}
			return result;
		}

		private List<Level> GetAllLevel()
		{
			List<Level> result;
			try
			{
				List<Element> elementByClass = YJKElementFilter.GetElementByClass(typeof(Level), this.doc);
				List<Level> list = new List<Level>();
				foreach (Element element in elementByClass)
				{
					Level level2 = element as Level;
					if (level2 != null)
					{
						list.Add(level2);
					}
				}
				list = (result = (from level in list
				orderby level.Elevation
				select level).ToList<Level>());
			}
			catch (Exception)
			{
				result = null;
			}
			return result;
		}

		public bool BandingTreeView(TreeView tv)
		{
			tv.CheckBoxes = true;
			TreeNode treeNode = TreeViewTools.CreatTreeViewNode("建筑物", "建筑物");
			List<Level> allLevel = this.GetAllLevel();
			if (allLevel == null)
			{
				return false;
			}
			List<Space> allSpaces = this.GetAllSpaces();
			if (allSpaces == null)
			{
				YJKMessageBox.Show("项目内没有搜索到空间，\n无法进行分区操作！");
				return false;
			}
			int num = 1;
			int num2 = 1001;
			foreach (Level level in allLevel)
			{
				TreeNode treeNode2 = TreeViewTools.CreatTreeViewNode(string.Concat(new object[]
				{
					num,
					"楼层[",
					level.Name,
					"]"
				}), level.Id.ToString());
				int num3 = 0;
				foreach (Space space in allSpaces)
				{
					Level hylevel = space.GetYJKLevel();
					if (hylevel != null && hylevel.Id == level.Id)
					{
						int num4 = space.Name.IndexOf(" ");
						TreeNode node = TreeViewTools.CreatTreeViewNode(string.Concat(new object[]
						{
							num2,
							"[",
							space.Name.Substring(num4 + 1),
							" ",
							space.Name.Substring(0, num4),
							"]"
						}), space.Id.ToString());
						treeNode2.Nodes.Add(node);
						num3++;
					}
				}
				if (num3 > 0)
				{
					treeNode.Nodes.Add(treeNode2);
					num2 = 1001 + num * 1000;
					num++;
				}
			}
			tv.Nodes.Add(treeNode);
			return true;
		}

		public Zone CreatHYZone(Document doc, string zoneName)
		{
			Zone result;
			try
			{
				List<Element> elementByCategory = YJKElementFilter.GetElementByCategory(doc, BuiltInCategory.OST_Levels);
				Level level = null;
				foreach (Element element in elementByCategory)
				{
					level = (element as Level);
					if (level != null)
					{
						break;
					}
				}
				ElementId elementId = doc.ActiveView.GetParameter(BuiltInParameter.VIEW_PHASE).AsElementId();
				Phase phase = doc.GetElement(elementId) as Phase;
				result = doc.Create.NewZone(level, phase);
			}
			catch (Exception)
			{
				throw;
			}
			return result;
		}

		public void SetZonePara(Zone zone, double xjWD, double djWD, double xjSD, double djSD, double freshAir)
		{
			zone.CoolingSetPoint = UnitConvert.CovertToAPI(xjWD, (Autodesk.Revit.DB.DisplayUnitType)57);
			zone.HeatingSetPoint = UnitConvert.CovertToAPI(djWD, (Autodesk.Revit.DB.DisplayUnitType)57);
			zone.DehumidificationSetPoint = UnitConvert.CovertToAPI(xjSD, (Autodesk.Revit.DB.DisplayUnitType)19);
			zone.HumidificationSetPoint = UnitConvert.CovertToAPI(djSD, (Autodesk.Revit.DB.DisplayUnitType)19);
		}

		private bool CompareZonePara(Zone zone, double xjWD, double djWD, double xjSD, double djSD, double freshAir)
		{
			bool result = true;
            if (zone.CoolingSetPoint != UnitConvert.CovertToAPI(xjWD, (Autodesk.Revit.DB.DisplayUnitType)57) || zone.HeatingSetPoint != UnitConvert.CovertToAPI(djSD, (Autodesk.Revit.DB.DisplayUnitType)57) || zone.DehumidificationSetPoint != UnitConvert.CovertToAPI(xjSD, (Autodesk.Revit.DB.DisplayUnitType)19) || zone.HumidificationSetPoint != UnitConvert.CovertToAPI(djSD, (Autodesk.Revit.DB.DisplayUnitType)19))
			{
				result = false;
			}
			return result;
		}

		private List<Zone> GetAllZone()
		{
			List<Zone> list = new List<Zone>();
			new List<Element>();
			foreach (Element element in YJKElementFilter.GetElementByCategory(this.doc, BuiltInCategory.OST_HVAC_Zones))
			{
				Zone zone = element as Zone;
				if (zone != null)
				{
					list.Add(zone);
				}
			}
			return list;
		}

		public Zone BeExsitZone(double xjWD, double djWD, double xjSD, double djSD, double freshAir)
		{
			Zone zone = null;
			Zone result;
			try
			{
				foreach (Zone zone2 in this.GetAllZone())
				{
					if (this.CompareZonePara(zone2, xjWD, djWD, xjSD, djSD, freshAir))
					{
						zone = zone2;
						break;
					}
				}
				result = zone;
			}
			catch (Exception)
			{
				result = null;
			}
			return result;
		}

		private Document doc;
	}
}
